var RIGHT=true;
var LEFT=false;
var UNIT_METERS = 6378100;
var UNIT_FEET = 20925524.9;
var UNIT_MI = 3963.1676;
	
function fixBearing(bearing)
{
	if (bearing<=0){bearing+=360.0;}
	if (bearing>360.0){bearing-=360.0;}
	return bearing;
}
	
function bearingTo(fromLat, fromLon, toLat, toLon)
{
	var lat1,lon1,lat2,lon2;
	lat1=deg2rad(fromLat);
	lon1=deg2rad(fromLon);
	lat2=deg2rad(toLat);
	lon2=deg2rad(toLon);
	//double dLat = lat2-lat1;
	var dLon = lon2-lon1;
	return fixBearing(rad2deg(Math.atan2(Math.sin(dLon)*Math.cos(lat2), Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon))));
}
	
function distanceTo(fromLat, fromLon, toLat, toLon, unit)
{
	var lat1,lon1,lat2,lon2;
	lat1=deg2rad(fromLat);
	lon1=deg2rad(fromLon);
	lat2=deg2rad(toLat);
	lon2=deg2rad(toLon);
	var dLat = lat2-lat1;
	var dLon = lon2-lon1;
	
	var a = Math.sin(dLat/2.0) * Math.sin(dLat/2.0) +
	Math.cos(lat1) * Math.cos(lat2) * 
	Math.sin(dLon/2.0) * Math.sin(dLon/2.0);
	return 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0-a)) * unit;
}

function turnTo(from, to)
{
	if ((to-from<180.0&&to-from>0.0) ||
		((360.0+to)-from<180.0&&(360.0+to)-from>0.0))
	{return RIGHT;}return LEFT;
}

function angleTo(from, to)
{
	var abss;
	abss=from-to;
	if (abss<0.0){abss*=-1.0;}
	if (abss<=180.0){return abss;}
	if ((360.0+to)-from<=180.0){return (360.0+to)-from;}
	if ((360.0+from)-to<=180.0){return (360.0+from)-to;}
	return 0.0;
}
function deg2rad(deg){
	return deg * (Math.PI/180);
}

function rad2deg(rad){
	return rad * (180/Math.PI);
}